home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / session.c < prev    next >
C/C++ Source or Header  |  1992-05-14  |  9KB  |  447 lines

  1. /* @(#) $Header: session.c,v 1.11 92/05/14 13:20:27 deyke Exp $ */
  2.  
  3. /* NOS User Session control
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "config.h"
  9. #include "mbuf.h"
  10. #include "proc.h"
  11. #include "tcp.h"
  12. #include "netuser.h"
  13. #include "ftp.h"
  14. #include "icmp.h"
  15. #include "telnet.h"
  16. #include "tty.h"
  17. #include "session.h"
  18. #include "hardware.h"
  19. #include "socket.h"
  20. #include "cmdparse.h"
  21. #include "commands.h"
  22. #include "main.h"
  23. #include "axclient.h"
  24. #include "finger.h"
  25. #include "netrom.h"
  26.  
  27. struct session *Sessions;
  28. struct session *Current;
  29. char Notval[] = "Not a valid control block\n";
  30. static char Badsess[] = "Invalid session\n";
  31.  
  32. static struct session *sessptr __ARGS((char *cp));
  33.  
  34. /* Convert a character string containing a decimal session index number
  35.  * into a pointer. If the arg is NULLCHAR, use the current default session.
  36.  * If the index is out of range or unused, return NULLSESSION.
  37.  */
  38. static struct session *
  39. sessptr(cp)
  40. char *cp;
  41. {
  42.     register struct session *sp;
  43.     unsigned int i;
  44.  
  45.     if(cp == NULLCHAR){
  46.         sp = Current;
  47.     } else {
  48.         i = (unsigned)atoi(cp);
  49.         if(i >= Nsessions)
  50.             sp = NULLSESSION;
  51.         else
  52.             sp = &Sessions[i];
  53.     }
  54.     if(sp == NULLSESSION || sp->type == FREE)
  55.         sp = NULLSESSION;
  56.  
  57.     return sp;
  58. }
  59.  
  60. /* Select and display sessions */
  61. int
  62. dosession(argc,argv,p)
  63. int argc;
  64. char *argv[];
  65. void *p;
  66. {
  67.     struct session *sp;
  68.  
  69.     sp = (struct session *)p;
  70.  
  71.     if(argc > 1){
  72.         if((Current = sessptr(argv[1])) != NULLSESSION){
  73.             go(0,NULL,sp);
  74.         } else
  75.             printf("Session %s not active\n",argv[1]);
  76.         return 0;
  77.     }
  78.     printf(" #       &CB Type   Rcv-Q  State        Remote socket\n");
  79.     for(sp=Sessions; sp < &Sessions[Nsessions];sp++){
  80.         switch(sp->type){
  81.         case TELNET:
  82.             printf("%c%-3d%8lx Telnet  %4d  %-13s%-s",
  83.              (Current == sp)? '*':' ',
  84.              (int)(sp - Sessions),
  85.              (long)sp->cb.telnet->tcb,
  86.              sp->cb.telnet->tcb->rcvcnt,
  87.              Tcpstates[sp->cb.telnet->tcb->state],
  88.              pinet_tcp(&sp->cb.telnet->tcb->conn.remote));
  89.             break;
  90.         case FTP:
  91.             printf("%c%-3d%8lx FTP     %4d  %-13s%-s",
  92.              (Current == sp)? '*':' ',
  93.              (int)(sp - Sessions),
  94.              (long)sp->cb.ftp->control,
  95.              sp->cb.ftp->control->rcvcnt,
  96.              Tcpstates[sp->cb.ftp->control->state],
  97.              pinet_tcp(&sp->cb.ftp->control->conn.remote));
  98.             break;
  99. #ifdef  AX25
  100.         case AX25TNC:
  101.             printf("%c%-3d%8lx AX25    %4d  %-13s%-s",
  102.              (Current == sp) ? '*' : ' ',
  103.              (int) (sp - Sessions),
  104.              (long) sp->cb.ax25,
  105.              sp->cb.ax25->rcvcnt,
  106.              ax25states[sp->cb.ax25->state],
  107.              ax25hdr_to_string(&sp->cb.ax25->hdr));
  108.             break;
  109. #endif
  110.         case FINGER:
  111.             printf("%c%-3d%8lx Finger  %4d  %-13s%-s",
  112.              (Current == sp)? '*':' ',
  113.              (int)(sp - Sessions),
  114.              (long)sp->cb.finger->tcb,
  115.              sp->cb.finger->tcb->rcvcnt,
  116.              Tcpstates[sp->cb.finger->tcb->state],
  117.              pinet_tcp(&sp->cb.finger->tcb->conn.remote));
  118.             break;
  119. #ifdef NETROM
  120.         case NRSESSION:
  121.             printf("%c%-3d%8lx NETROM  %4d  %-13s%-s",
  122.              (Current == sp) ? '*' : ' ',
  123.              (int) (sp - Sessions),
  124.              (long) sp->cb.netrom,
  125.              sp->cb.netrom->rcvcnt,
  126.              ax25states[sp->cb.netrom->state],
  127.              nr_addr2str(sp->cb.netrom));
  128.             break;
  129. #endif
  130.         default:
  131.             continue;
  132.         }
  133.         if(sp->rfile != NULLCHAR)
  134.             printf("    Record: %s ",sp->rfile);
  135.         if(sp->ufile != NULLCHAR)
  136.             printf("    Upload: %s",sp->ufile);
  137.         printf("\n");
  138.     }
  139.     return 0;
  140. }
  141. /* Enter conversational mode with current session */
  142. int
  143. go(argc,argv,p)
  144. int argc;
  145. char *argv[];
  146. void *p;
  147. {
  148.     if(Current == NULLSESSION || Current->type == FREE)
  149.         return 0;
  150.     Mode = CONV_MODE;
  151.     switch(Current->type){
  152.     case TELNET:
  153.         if(Current->cb.telnet->remote[TN_ECHO])
  154.             raw();  /* Re-establish raw mode if it was set */
  155.         rcv_char(Current->cb.telnet->tcb,0); /* Get any pending input */
  156.         break;
  157.     case FTP:
  158.         ftpccr(Current->cb.ftp->control,0);
  159.         break;
  160. #ifdef  AX25
  161.     case AX25TNC:
  162.         axclient_recv_upcall(Current->cb.ax25,0);
  163.         break;
  164. #endif
  165.     case FINGER:
  166.         fingcli_rcv(Current->cb.finger->tcb,0) ;
  167.         break ;
  168. #ifdef  NETROM
  169.     case NRSESSION:
  170.         nrclient_recv_upcall(Current->cb.netrom,0);
  171.         break;
  172. #endif
  173.     }
  174.     return 0;
  175. }
  176. int
  177. doclose(argc,argv,p)
  178. int argc;
  179. char *argv[];
  180. void *p;
  181. {
  182.     struct session *sp;
  183.  
  184.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  185.         printf(Badsess);
  186.         return -1;
  187.     }
  188.     switch(sp->type){
  189.     case TELNET:
  190.         close_tcp(sp->cb.telnet->tcb);
  191.         break;
  192.     case FTP:
  193.         close_tcp(sp->cb.ftp->control);
  194.         break;
  195. #ifdef  AX25
  196.     case AX25TNC:
  197.         close_ax(sp->cb.ax25);
  198.         break;
  199. #endif
  200.     case FINGER:
  201.         close_tcp(sp->cb.finger->tcb);
  202.         break;
  203. #ifdef NETROM
  204.     case NRSESSION:
  205.         close_nr(sp->cb.netrom);
  206.         break;
  207. #endif
  208.     }
  209.     return 0;
  210. }
  211. int
  212. doreset(argc,argv,p)
  213. int argc;
  214. char *argv[];
  215. void *p;
  216. {
  217.     struct session *sp;
  218.  
  219.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  220.         printf(Badsess);
  221.         return -1;
  222.     }
  223.     switch(sp->type){
  224.     case TELNET:
  225.         reset_tcp(sp->cb.telnet->tcb);
  226.         break;
  227.     case FTP:
  228.         if(sp->cb.ftp->data != NULLTCB){
  229.             reset_tcp(sp->cb.ftp->data);
  230.             sp->cb.ftp->data = NULLTCB;
  231.         }
  232.         reset_tcp(sp->cb.ftp->control);
  233.         break;
  234. #ifdef  AX25
  235.     case AX25TNC:
  236.         reset_ax(sp->cb.ax25);
  237.         break;
  238. #endif
  239.     case FINGER:
  240.         reset_tcp(sp->cb.finger->tcb);
  241.         break;
  242. #ifdef NETROM
  243.     case NRSESSION:
  244.         reset_nr(sp->cb.netrom);
  245.         break;
  246. #endif
  247.     }
  248.     return 0;
  249. }
  250. int
  251. dokick(argc,argv,p)
  252. int argc;
  253. char *argv[];
  254. void *p;
  255. {
  256.     struct session *sp;
  257.  
  258.     if((sp = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  259.         printf(Badsess);
  260.         return -1;
  261.     }
  262.     switch(sp->type){
  263.     case TELNET:
  264.         if(kick_tcp(sp->cb.telnet->tcb) == -1){
  265.             printf(Notval);
  266.             return 1;
  267.         }
  268.         break;
  269.     case FTP:
  270.         if(kick_tcp(sp->cb.ftp->control) == -1){
  271.  
  272.             printf(Notval);
  273.             return 1;
  274.         }
  275.         if(sp->cb.ftp->data != NULLTCB)
  276.             kick_tcp(sp->cb.ftp->data);
  277.         break;
  278. #ifdef  AX25
  279.     case AX25TNC:
  280.         if(kick_ax(sp->cb.ax25) == -1){
  281.             printf(Notval);
  282.             return 1;
  283.         }
  284.         break;
  285. #endif
  286.     case FINGER:
  287.         if(kick_tcp(sp->cb.finger->tcb) == -1){
  288.             printf(Notval);
  289.             return 1;
  290.         }
  291.         break;
  292. #ifdef NETROM
  293.     case NRSESSION:
  294.         if(kick_nr(sp->cb.netrom) == -1){
  295.             printf(Notval);
  296.             return 1;
  297.         }
  298.         break;
  299. #endif
  300.     }
  301.     return 0;
  302. }
  303. struct session *
  304. newsession()
  305. {
  306.     register int i;
  307.  
  308.     for(i=0;i<Nsessions;i++)
  309.         if(Sessions[i].type == FREE)
  310.             return &Sessions[i];
  311.     return NULLSESSION;
  312. }
  313. void
  314. freesession(sp)
  315. struct session *sp;
  316. {
  317.     if(sp == NULLSESSION)
  318.         return;
  319.     if(sp->record != NULLFILE){
  320.         fclose(sp->record);
  321.         sp->record = NULLFILE;
  322.     }
  323.     if(sp->rfile != NULLCHAR){
  324.         free(sp->rfile);
  325.         sp->rfile = NULLCHAR;
  326.     }
  327.     if(sp->upload != NULLFILE){
  328.         fclose(sp->upload);
  329.         sp->upload = NULLFILE;
  330.     }
  331.     if(sp->ufile != NULLCHAR){
  332.         free(sp->ufile);
  333.         sp->ufile = NULLCHAR;
  334.     }
  335.     if(sp->name != NULLCHAR){
  336.         free(sp->name);
  337.         sp->name = NULLCHAR;
  338.     }
  339.     sp->type = FREE;
  340.     memset(sp, 0, sizeof(struct session));
  341.     if(sp == Current)
  342.         Current = NULLSESSION;
  343. }
  344. /* Control session recording */
  345. int
  346. dorecord(argc,argv,p)
  347. int argc;
  348. char *argv[];
  349. void *p;
  350. {
  351.     if(Current == NULLSESSION){
  352.         printf("No current session\n");
  353.         return 1;
  354.     }
  355.     if(argc > 1){
  356.         if(Current->rfile != NULLCHAR){
  357.             fclose(Current->record);
  358.             free(Current->rfile);
  359.             Current->record = NULLFILE;
  360.             Current->rfile = NULLCHAR;
  361.         }
  362.         /* Open new record file, unless file name is "off", which means
  363.          * disable recording
  364.          */
  365.         if(strcmp(argv[1],"off") != 0
  366.          && (Current->record = fopen(argv[1],"a")) != NULLFILE){
  367.             Current->rfile = malloc((unsigned)strlen(argv[1])+1);
  368.             strcpy(Current->rfile,argv[1]);
  369.         }
  370.     }
  371.     if(Current->rfile != NULLCHAR)
  372.         printf("Recording into %s\n",Current->rfile);
  373.     else
  374.         printf("Recording off\n");
  375.     return 0;
  376. }
  377. /* Control file transmission */
  378. int
  379. doupload(argc,argv,p)
  380. int argc;
  381. char *argv[];
  382. void *p;
  383. {
  384.     struct tcb *tcb;
  385.     struct ax25_cb *axp;
  386.     struct circuit *cb;
  387.  
  388.     if(Current == NULLSESSION){
  389.         printf("No current session\n");
  390.         return 1;
  391.     }
  392.     if(argc > 1){
  393.         switch(Current->type){
  394.         case FTP:
  395.             printf("Uploading on FTP control channel not supported\n");
  396.             return 1;
  397.         case FINGER:
  398.             printf("Uploading on FINGER session not supported\n");
  399.             return 1;
  400.         }
  401.         /* Abort upload */
  402.         if(Current->upload != NULLFILE){
  403.             fclose(Current->upload);
  404.             Current->upload = NULLFILE;
  405.         }
  406.         if(Current->ufile != NULLCHAR){
  407.             free(Current->ufile);
  408.             Current->ufile = NULLCHAR;
  409.         }
  410.         if(strcmp(argv[1],"stop") != 0){
  411.             /* Open upload file */
  412.             if((Current->upload = fopen(argv[1],"r")) == NULLFILE){
  413.                 printf("Can't read %s\n",argv[1]);
  414.                 return 1;
  415.             }
  416.             Current->ufile = malloc((unsigned)strlen(argv[1])+1);
  417.             strcpy(Current->ufile,argv[1]);
  418.             /* All set, kick transmit upcall to get things rolling */
  419.             switch(Current->type){
  420. #ifdef  AX25
  421.             case AX25TNC:
  422.                 axp = Current->cb.ax25;
  423.                 axclient_send_upcall(axp, space_ax(axp));
  424.                 break;
  425. #endif
  426. #ifdef NETROM
  427.             case NRSESSION:
  428.                 cb = Current->cb.netrom;
  429.                 nrclient_send_upcall(cb, space_nr(cb));
  430.                 break;
  431. #endif
  432.             case TELNET:
  433.                 tcb = Current->cb.telnet->tcb;
  434.                 if(tcb->snd.wnd > tcb->sndcnt)
  435.                     (*tcb->t_upcall)(tcb,tcb->snd.wnd - tcb->sndcnt);
  436.                 break;
  437.             }
  438.         }
  439.     }
  440.     if(Current->ufile != NULLCHAR)
  441.         printf("Uploading %s\n",Current->ufile);
  442.     else
  443.         printf("Uploading off\n");
  444.     return 0;
  445. }
  446.  
  447.